package com.sanpu.kxe.business.login.presenter;

import android.text.TextUtils;

import com.sanpu.kxe.business.login.contract.ModifyAccountContract;
import com.sanpu.kxe.business.login.model.LoginModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.utils.AppContextUtil;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：修改登录手机号
 * Created by 薛勇军 on 2019/3/22.
 */

public class ModifyAccountPresenter implements ModifyAccountContract.Presenter {

    private ModifyAccountContract.View mView;

    private CompositeSubscription mSubscription;

    public ModifyAccountPresenter(ModifyAccountContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void updateAccount(String newAccount, String checkCode, String name, String identityNumber, String password) {
        if (TextUtils.isEmpty(newAccount)) {
            ToastUtil.showTextViewPrompt("新手机号不能为空");
            return;
        }
        if (VerifyUtil.checkPhoneNumber(newAccount)) {
            ToastUtil.showTextViewPrompt("请输入正确的新手机号");
            return;
        }
        if (TextUtils.isEmpty(checkCode)) {
            ToastUtil.showTextViewPrompt("短信验证码不能为空");
            return;
        }
        if (TextUtils.isEmpty(name)) {
            ToastUtil.showTextViewPrompt("必须填写名字");
            return;
        }
        if (TextUtils.isEmpty(identityNumber)) {
            ToastUtil.showTextViewPrompt("必须填写身份证号");
            return;
        }
        if (VerifyUtil.checkIdValidation(identityNumber)) {
            ToastUtil.showTextViewPrompt("身份证号填写不正确");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showTextViewPrompt("请输入原密码");
            return;
        }
        mView.showLoadingDialog(false);
        Subscription subscription = LoginModel.getInstance()
                .modifyUserName(newAccount, checkCode, name, identityNumber, password)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(String s) {
                        ToastUtil.showTextViewPrompt("修改登录手机号成功，请用新手机号登录！");
                        AppContextUtil.exit(mView.getContext());
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onSendCode(final String phoneNumber) {
        Observable.interval(0, 1, TimeUnit.SECONDS)//设置0延迟，每隔一秒发送一条数据
                .take(CommonConfig.SMS_SEC + 1) //设置循环11次
                .map(new Func1<Long, Long>() {
                    @Override
                    public Long call(Long aLong) {
                        return CommonConfig.SMS_SEC - aLong;
                    }
                })
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mView.changeByStatus(true);
                        Subscription subscription = LoginModel.getInstance()
                                .sendSms(phoneNumber, CommonConfig.SMSTYPE_MODACCOUNT)
                                .subscribeOn(Schedulers.io())
                                .unsubscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Observer<String>() {
                                    @Override
                                    public void onCompleted() {

                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        mView.showErrorText(e);
                                    }

                                    @Override
                                    public void onNext(String s) {
                                        ToastUtil.showTextViewPromptShort("验证码发送成功！");
                                    }
                                });
                        mSubscription.add(subscription);
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onCompleted() {
                        mView.changeByStatus(false);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Long aLong) { //接受到一条就是会操作一次UI
                        mView.byoyomi(aLong);
                    }
                });
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
